package cn.com.haopy.yue.ai.db.service.hos.impl;

import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskTopic;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskTopicMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskTopicService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 9:13
 */
@Service
public class AIRiskTopicServiceImpl extends BaseServiceImpl<AIRiskTopicMapper, AIRiskTopic> implements AIRiskTopicService {

    @Override
    public Page<AIRiskTopic> querySelective(String topic, Integer isActive, Integer page, Integer size, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }
        QueryWrapper<AIRiskTopic> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(topic)) {
            queryWrapper.lambda().like(AIRiskTopic::getTopic, topic);
        }
        if (Objects.nonNull(isActive)) {
            queryWrapper.lambda().eq(AIRiskTopic::getIsActive, isActive);
        }
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, size), queryWrapper);
    }

    @Override
    public List<AIRiskTopic> getByTopic(String topic) {
        QueryWrapper<AIRiskTopic> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskTopic::getTopic, topic);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<AIRiskTopic> listAll() {
        return super.list();
    }

    @Override
    public List<AIRiskTopic> listActiveAll() {
        QueryWrapper<AIRiskTopic> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskTopic::getIsActive, Boolean.TRUE);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<AIRiskTopic> listActiveSexAll(Short sex) {
        QueryWrapper<AIRiskTopic> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskTopic::getIsActive, Boolean.TRUE);
        queryWrapper.lambda().and(wrapper -> {
            wrapper.eq(AIRiskTopic::getSex, 0).or().eq(AIRiskTopic::getSex, sex);
        });
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<AIRiskTopic> listActiveSexByIds(Short sex, List<Integer> ids) {
        QueryWrapper<AIRiskTopic> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskTopic::getIsActive, Boolean.TRUE);
        queryWrapper.lambda().and(wrapper -> {
            wrapper.eq(AIRiskTopic::getSex, 0).or().eq(AIRiskTopic::getSex, sex);
        });
        queryWrapper.lambda().in(AIRiskTopic::getId, ids);
        return baseMapper.selectList(queryWrapper);
    }
}
